<?php  
	namespace Home\Controller;
	use Think\Controller;

	class CategoryController extends Controller
	{
		public function index()
		{
			//实例化model  
	        $type = M('goods_type');

	        $goodtype = $type->field('id,type_name')->where('pid=0')->limit(5)->select();
	        $prodect = M('product');
	        foreach ($goodtype as $k => $value) {
	        	$a=$value['id'];
	        	$goodstype[] = $type->field('type_name,id,img')->where("pid=$a")->select();
	        	foreach ($goodstype as $key => $val) {
	        		
	        	}
	        }

	        // NSLog($goodstype);
	        $this->assign('goodstype',$goodstype);  

	        
         	$product  =M('product');
	        $parent = $type->where('pid=0')->select();
	    
	            //pid=>父类名
            foreach ($parent as $k => $v)
            {     
               	$sec = $type->where('pid='.$v['id'])->select();
            	$parent[$k]['child']= $type->where('pid='.$v['id'])->select();

            	foreach($sec as $key => $thdval){
            		// $child[]=$product ->alias('pro')->join('__SKU_GOODS__ sg ON pro.max_id = sg.proudct_id')->where(['cat_id'=>$thdval['id']])->select();
            		// $child[]=$product -> where('cat_id='.$thdval['id'])->select();
            		// $parent[$k]['child'][$key]['product'] = $product -> where(['cat_id'=>$thdval['id']])->select();
            		$parent[$k]['child'][$key]['product'] = $product ->alias('pro')->join('__SKU_GOODS__ sg ON pro.max_id = sg.proudct_id')->where(['cat_id'=>$thdval['id']])->select();
            	}
            	

            }
        // NSLog($parent);
	        
	        $this->assign('list',$parent);

	        $this->display();
		}
	}